<?php

/*
 * Edit by shengxin
 * Click nbfs://nbhost/SystemFileSystem/Templates/Scripting/PHPClass.php to edit this template
 */
namespace api\mall\controller;

use api\mall\model\UserMoveModel;
use api\mall\model\AddressModel;
use app\user\model\UserModel;

use cmf\controller\RestBaseController;
/**
 * Description of User
 *
 * @author PC
 */
class UserController extends RestBaseController{
    
    protected function initialize() {
        parent::initialize();
        if(empty($this->user)){
            $this->error('login');
        }
    }
    
    public function move(){
        $now = time();
        $user = $this->user;
        $data = $this->request->post(); 
//        $result  = $this->validate(
//            $position,
//            [
//                'x' => 'require|integer',
//                'y' => 'require|integer'
//                
//
//            ],
//            [
//                'x.require' => '坐标x！',
//                'x.integer'     => '省份名称超过最大长度！！',
//                
//                'y.require' => '坐标y！',
//                'y.integer'     => '所在市名称超过最大长度！！'
//              
//            ]
//        );

//        if (true !== $result) {
//            // 验证失败 输出错误信息
//            $this->error($result);
//        }
        
        $position = [
            'x'=> empty($data['x'])?0:$data['x'],            
            'y'=>empty($data['y'])?0:$data['y']
        ];
        $position['uid'] = $user->id;       
        $position['user_nickname'] = $user->user_nickname;
        $position['update_time'] = $now;
        $model = new UserMoveModel();
        $model->duplicate($position)->save($position);
        //我敢认定同时在线的人不会太多
        //所以这里就这么干
        $list = $model->field('uid,x,y,user_nickname')->where('update_time','>',$now-100)->limit(30)->select();
        $this->success('ok', $list);
    }
    
    public function editUser(){
        $data = $this->request->post();
        $result  = $this->validate(
            $data,
            [
                'user_nickname' => 'string|length:2,20',   
                'user_pass' => 'string|length:8,20',   
                'mobile' => 'mobile',
                'code'=>'length:4'
//                'avatar' => 'require|integer'
                

            ],
            [
                'mobile.mobile' => '手机号码不正确！',
                'user_nickname.length' => '名称超过最大长度！！',
            ]
        );

        if (true !== $result) {
            // 验证失败 输出错误信息
            $this->error($result);
        }
        
        $update = [];
        if (isset($data['user_pass'])){
            $update['user_pass'] = cmf_password($data['user_pass']);
        }
        if (isset($data['user_nickname'])){
            $update['user_nickname'] = $data['user_nickname'];
        }
        if (isset($data['mobile'])){
            if(empty($data['code'])){
                $this->error('验证码不能为空');
            }
            $result = cmf_check_verification_code($data['mobile'], $data['code']);
            if($result!==true){
                $this->error($result);
            }
            $update['mobile'] = $data['mobile'];            
        }
        if(empty($update)){
            $this->success('干啥呢');
        }
        $umodel = new UserModel();
        $umodel->where('id',$this->userId)->update($update);
        $this->success('ok');
    }
    
    
    public function address(){
        $addressModel = new AddressModel();
        $address = $addressModel->where('uid',$this->userId)->select();
        $this->success('ok',$address);
    }
    
    public function editAddress(){
        $data = $this->request->post();
        
        $result  = $this->validate(
            $data,
            [
                'province' => 'require|max:20',
                'city' => 'require|max:20',
                'district' => 'require|max:20',
                'real_name' => 'require|max:20',
                'phone_number' => 'require|mobile',
                'detail' => 'require|max:32',                
                'is_default' => 'integer',

            ],
            [
                'province.require' => '省份必填！',
                'province.max'     => '省份名称超过最大长度！！',
                
                'city.require' => '所在市必填！',
                'city.max'     => '所在市名称超过最大长度！！',
                
                'district.require' => '区必填！',
                'district.max'     => '区名称超过最大长度！！',
                
                'real_name.require' => '收货人姓名必填！',
                'real_name.max'     => '姓名超过最大长度！！',
                
                'detail.require' => '详细收货地址必填！',
                'detail.max'     => '收货地址超过最大长度！！',
                
                'phone_number.require' => '手机号必填！',
                'phone_number.mobile'     => '手机号码不正确！！',
            ]
        );

        if (true !== $result) {
            // 验证失败 输出错误信息
            $this->error($result);
        }
        $addrModel = new AddressModel();
        $data['uid']= $this->userId;
        $data['update_time'] = time();
        if(isset($data['id'])){
            $addrModel->where('uid',$this->userId)->where('id',$data['id'])->update($data);
        }else{
            $data['create_time'] = time();
            $addrModel->insert($data);
        }
        $this->success('ok',$data);
    }
    
}
